package net.dgg.zz.production.abnormal.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.tmd.foundation.platform.annotation.AuthOpt;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.zz.common.service.CommonMangerExtService;
import net.dgg.zz.common.utils.DateUtils;
import net.dgg.zz.common.utils.OrderHandleUtils;
import net.dgg.zz.common.utils.excel.ExcelUtils;
import net.dgg.zz.flownode.dao.ZzProductNodeDao;
import net.dgg.zz.flownode.dao.ZzProductNodeFlowDao;
import net.dgg.zz.production.abnormal.service.ZzAbnormalOrderService;
import net.dgg.zz.production.inProcess.entity.ProductNode;
import net.dgg.zz.production.inProcess.entity.ProductNodeFlow;
import net.dgg.zz.production.vo.AbnormalOrderVo;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * @author 孙上峰
 * @despriction 异常订单查询Contoller
 * @create 2018/5/19 0019 下午 3:18
 **/
@Controller
@RequestMapping("/zzsc/abnormal_order")
public class ZzAbnormalOrderController extends ZzBaseController {
    @Autowired
    private ZzAbnormalOrderService zzAbnormalOrderService;
    @Autowired
    private CommonMangerExtService commonMangerExtService;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private ZzProductNodeFlowDao zzProductNodeFlowDao;
    @Autowired
    private ZzProductNodeDao zzProductNodeDao;
    /**
     * z
     * 跳转到异常订单列表页
     */
    @RequestMapping("/index")
    public String toDeptAbnormalOrderPage(ModelMap modelMap, String classify) {
       /* classify = classify == null ? "全部" : classify;
        Map statusMap = new LinkedHashMap();

        statusMap.put("全部", "");
        statusMap.put("下单待接收", ScProductConstant.SC_TO_RECEIVE_XDDJS);
        statusMap.put("转单待接收", ScProductConstant.SC_TO_RECEIVE_ZDDJS);
        statusMap.put("分配待接收", ScProductConstant.SC_TO_RECEIVE_FPDJS);
        statusMap.put("移交待接收", ScProductConstant.SC_TO_RECEIVE_YJDJS);
        statusMap.put("办理中", ScProductConstant.SC_ORDER_IN_PROCESS);
        statusMap.put("生产暂停", ScProductConstant.SC_ORDER_SUSPEND);
        statusMap.put("退单审核中", ScProductConstant.SC_ORDER_CHARGEBACK_DSH);
        statusMap.put("退回待接收", ScProductConstant.SC_TO_RECEIVE_TDDJS);*/
        List<ProductNode> listNode = new ArrayList<>();
        //查询出当前登陆人的事业部id
        Map orgMap = commonMangerExtService.findBusDeptById(sessionManager.getCurrentUser().getOrgId());
        if (orgMap != null && orgMap.size() != 0) {
            Map map = new HashMap();
            Map mapNode = new HashMap();
            StringBuilder ids = new StringBuilder();
            //根据事业部的id查询出改事业部配置流程节点ids
            map.put("deptIds", orgMap.get("id"));
            if (null == map.get("orderColumn")) {
                map.put("orderDir", "desc");
                map.put("orderColumn", "updateTime");
            }
            List<ProductNodeFlow> productNodeFlowList = zzProductNodeFlowDao.query(map);
            if (productNodeFlowList.size() > 0) {
                for (ProductNodeFlow productNodeFlow : productNodeFlowList) {
                    if (ids.length() > 0) {//该步即不会第一位有逗号，也防止最后一位拼接逗号！
                        ids.append(",");
                    }
                    ids.append(productNodeFlow.getId());
                }
                mapNode.put("deptIds", orgMap.get("id"));
                mapNode.put("processId", ids.toString().split(","));
                listNode = zzProductNodeDao.queryByFlowProcessIds(mapNode);
            }
        }
        modelMap.addAttribute("listNode", listNode);
        // modelMap.addAttribute("statusMap", statusMap);
        modelMap.addAttribute("classify", classify);
        return "zzsc/abnormal/page_order_abnormal";
    }


    /**
     * 查询异常订单列表数据
     */
    @RequestMapping("/query_order")
    @ResponseBody
    public DataTableResponse queryDeptAbnormalOrder(@RequestParam Map params, HttpServletRequest request) {
        DataTableRequest r = this.generatePatameter(params, request);
        Map fileMap = new HashMap();
        fileMap.put("signTime", "signTime");
        fileMap.put("lastRemarkTime", "lastRemarkTime");
        params.put("start", r.getStart());
        params.put("limit", r.getLength());
        OrderHandleUtils.getOrderCol(r, params, fileMap);
        List<Map> list = zzAbnormalOrderService.query(params);
        int total = zzAbnormalOrderService.count(params);
        return new DataTableResponse(r.getDraw(), total, total, list, "");

    }

    /**
     * 导出数据
     */
    @RequestMapping("/export_sczz_abnormal")
    public void exportSczzAbnormal(@RequestParam Map params, HttpServletResponse response) {
        try {
            String fileName = "异常订单_" + DateUtils.dateString(new Date(), "yyyyMMddHHmm") + ".xlsx";
            List<AbnormalOrderVo> dataList = zzAbnormalOrderService.findByParamsList(params);
            ExcelUtils.writeToFile(dataList, null, fileName, response,AbnormalOrderVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/layer_turnover_abnormal")
    public String turnoverAbnormalLayer(@RequestParam String ids, ModelMap modelMap) {
        modelMap.addAttribute("ids", ids);
        return "zzsc/abnormal/layer_turnover_abnormal";
    }

    /**
     * 移交异常订单
     *
     * @param paras
     * @return
     */
    @NLog
    @AuthOpt(code = "ZZSC_TURNOVER_ABNORMAL")
    @RequestMapping("/turnover_abnormal")
    @ResponseBody
    public DggRestResponse turnoverAbnormal(@RequestParam Map paras) {

        try {
            zzAbnormalOrderService.turnoverAbnormal(paras);
            return this.getSuccessResponse(null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return this.getFailResponse("移交失败");
        }

    }

}
